using System.Linq;
using LocalOverride.Fiddler.Store;
using NSubstitute;
using NUnit.Framework;

namespace LocalOverride.Tests.UrlMapping
{
    [TestFixture]
    public class MappingEntryValidatorTests
    {
        [Test]
        public void InvalidUrl_ReturnsInvalidUrlMessage()
        {
            var entry = new MappingEntry
                            {
                                Url = "http:2222///blah.com",
                                LocalPath = @"c:\test.html"
                            };

            var localFileAccess = Substitute.For<ILocalFileAccess>();

            var validator = new MappingEntryValidator(entry, localFileAccess);

            var result = validator.Validate();
            Assert.IsFalse(result.IsValid);
            var error = result.Errors.First();
            Assert.AreEqual("Url", error.Field);
            Assert.AreEqual("Invalid Url", error.Message);
        }

        [Test]
        public void BlankUrl_ReturnsRequiredMessage()
        {
            var entry = new MappingEntry
            {
                Url = "",
                LocalPath = @"c:\test.html"
            };

            var localFileAccess = Substitute.For<ILocalFileAccess>();

            var validator = new MappingEntryValidator(entry, localFileAccess);

            var result = validator.Validate();
            Assert.IsFalse(result.IsValid);
            var error = result.Errors.First();
            Assert.AreEqual("Url", error.Field);
            Assert.AreEqual("Url is required", error.Message);
        }

        [Test]
        public void InvalidFilePath_ReturnsErrorMessage()
        {

            var localFileAccess = Substitute.For<ILocalFileAccess>();
            localFileAccess.IsValidFilename(@"blah").Returns(false);
            
            var entry = new MappingEntry
                            {
                                Url = "http://test.com",
                                LocalPath = @"blah",
                                IsDirectory = false
                            };

            var validator = new MappingEntryValidator(entry, localFileAccess);

            var result = validator.Validate();
            Assert.IsFalse(result.IsValid);

            var error = result.Errors.First();
            Assert.AreEqual("LocalPath", error.Field);
            Assert.AreEqual("Invalid File Path", error.Message);
        }

        [Test]
        public void InvalidDirectory_ReturnsErrorMessage()
        {

            var localFileAccess = Substitute.For<ILocalFileAccess>();
            localFileAccess.IsValidDirectory(@"blah").Returns(false);
            
            var entry = new MappingEntry
            {
                Url = "http://test.com",
                LocalPath = @"blah",
                IsDirectory = true
            };

            var validator = new MappingEntryValidator(entry, localFileAccess);
            
            var result = validator.Validate();

            Assert.IsFalse(result.IsValid);

            var error = result.Errors.First();
            Assert.AreEqual("LocalPath", error.Field);
            Assert.AreEqual("Invalid Directory Path", error.Message);
        }
    }
}